import { defineStore } from 'pinia'
import { departApi as deptMerchant } from '/@/api/system/b-merchant/depart-api'
import { departApi as deptOperation } from '/@/api/system/b-operation/depart-api'
import { departApi as deptAgent } from '/@/api/system/b-agent/depart-api'

const useCommonStore = defineStore('common', {
  state: () => ({
    deptTreeAgent: [],
    deptTreeOperation: [],
    deptTreeMerchant: [],
  }),
  actions: {
    // 获取代理商部门树数据
    getDeptTreeAgent() {
      return new Promise((resolve) => {
        deptAgent.tree().then((res) => {
          this.deptTreeAgent = res.data
          resolve(this.deptTreeAgent)
        })

        // if (!this.deptTreeAgent.length) {
        //   deptAgent.tree().then((res) => {
        //     this.deptTreeAgent = res.data
        //     resolve(this.deptTreeAgent)
        //   })
        // } else {
        //   resolve(this.deptTreeAgent)
        // }
      })
    },
    // 获取平台部门树数据
    getDeptTreeOperation() {
      return new Promise((resolve) => {
        if (!this.deptTreeOperation.length) {
          deptOperation.tree().then((res) => {
            this.deptTreeOperation = res.data
            resolve(this.deptTreeOperation)
          })
        } else {
          resolve(this.deptTreeOperation)
        }
      })
    },
    // 获取商户部门树数据
    getDeptTreeMerchant() {
      return new Promise((resolve) => {
        if (!this.deptTreeMerchant.length) {
          deptMerchant.tree().then((res) => {
            this.deptTreeMerchant = res.data
            resolve(this.deptTreeMerchant)
          })
        } else {
          resolve(this.deptTreeMerchant)
        }
      })
    },
  },
})

export default useCommonStore
